package com.pickup.rabbitMQ.comtroller;

import com.pickup.enums.MsgSignFlagEnum;
import com.pickup.netty.message.ChatMessage;
import com.pickup.netty.message.NettyData;
import com.pickup.pool.ThreadPoolUtil;
import com.pickup.rabbitMQ.consumer.RabbitMqConsumer;

import com.pickup.rabbitMQ.provider.RabbitMqProvider;
import com.pickup.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class RabbitMqController {

    @Autowired
    private RabbitMqProvider rabbitMqProvider;

    @Autowired
    private RabbitMqConsumer rabbitMqConsumer;

    @Autowired
    private UserService userService;

    @GetMapping("/sendMessage")
    public void sendMessage(@RequestParam(value = "message") String message, @RequestParam(value = "routKey") String routKey) {
        for (int i = 0; i < 100; i++) {
            rabbitMqProvider.sendMessage("0", message);
            rabbitMqProvider.sendMessage("1", message);
        }
    }

    @GetMapping("/pollMessage")
    public void pollMessage(@RequestParam(value = "routKey") String routKey) throws Exception {
        String key = "ChatMessage" + Math.abs(routKey.hashCode()) % 2;
        HashSet<String> set = rabbitMqConsumer.consumerConfig(key, routKey);
    }


    @GetMapping("/sendTestCru")
    public void send() {


        ThreadPoolUtil threadPoolUtil = new ThreadPoolUtil();

        try {
            long start = System.currentTimeMillis();
            System.out.println("开始时间：      ");
            for (int i = 0; i < 1000000000; i++) {
                TestSend();
//                threadPoolUtil.getExecutorPool().execute(()->{
//
//                });
            }

            long end = System.currentTimeMillis();
            System.out.println("结束时间：      "+(end - start) / 1000);
        } catch (Exception e) {
            System.out.println("报错；  "+e.getMessage());
        }

    }

    public void TestSend() {
        ChatMessage chatMsg = ChatMessage.builder()
                .receiverId("2203156D8G4XFZDG2W0")
                .senderId("2203156D8G4XFZDG2W1")
                .msg("")
                .msgType("image").build();
        NettyData nettyData = NettyData.builder()
                .action("SingleChatSendImage")
                .chatMessage(chatMsg)
                .build();
        userService.saveMsg(chatMsg, MsgSignFlagEnum.unsign.type);
        rabbitMqProvider.sendMessage(chatMsg.getReceiverId(), chatMsg.getReceiverId() + ",0");
    }
}
